﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;
using Pasys.PresentationCore;

namespace Generator.Generators
{
    public partial class MVCView : GeneratorCodeBase
    {
        //private
        //private ICommandBusService commandBus;

        //public IConnection Connection { get; set; }
        //private TableInfo _table;
        //public TableInfo Table
        //{
        //    get
        //    {
        //        return _table;
        //    }
        //    set
        //    {
        //        _table = value;
        //        if (value != null)
        //        {
        //            this.Text = string.Format("{0}", value.TableName);
        //        }
        //    }
        //}

        //public void AddToolStripItem(ToolStripItem item)
        //{

        //}

        //public string GeneratorName { get; set; }

        public MVCView()
        {
            InitializeComponent();
            this.GeneratorName = "MVC View Generator";
            AddToolItems();
        }

        private void AddToolItems()
        {
            ToolStripMenuItem item0 = new ToolStripMenuItem("List View");
            item0.Tag = 0;
            ToolStripMenuItem item1 = new ToolStripMenuItem("Edit View");
            item1.Tag = 1;

            this.AddToolStripItem(item0);
            this.AddToolStripItem(item1);
        }


        public override void Generate()
        {
            base.Generate();
        }

        public override void Generate(ToolStripItem item)
        {
            base.Generate(item);

            //this.Table = MYSQLUtility.GetTableInfo(this.Connection, this.Table.TableName);
            txtCode.ResetText();
            CodeBuilder.Clear();
            switch (Convert.ToInt32(item.Tag))
            {
                case 0:
                    if (this.Table.IsMasterTable)
                        create_masterlist();
                    else
                        create_commonlist();
                    break;
                case 1:
                    if (this.Table.IsMasterTable)
                        create_masteredit();
                    else
                        create_commonedit();
                    break;
                default:
                    break;
            }

            txtCode.Text = CodeBuilder.ToString();
            txtCode.Refresh();
        }

        #region master list
        private void create_masterlist()
        {
            create_masterlistdeclare();
            create_masterlistContent1();
            create_masterlistContent2();
        }
        private void create_masterlistdeclare()
        {
            CodeBuilder.AppendLine("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/Divided2Layout.Master\" Inherits=\"System.Web.Mvc.ViewPage<MvcApplication.Models.{0}>\" %>", this.Table.ModelListSearchName);
        }

        private void create_masterlistContent1()
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);

            CodeBuilder.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"ContentPlaceLeft\" runat=\"server\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <script type=\"text/javascript\">");
            CodeBuilder.AppendLine("        $(document).ready(function () {");
            CodeBuilder.AppendLine("            $('#dialog-edit').dialog({");
            CodeBuilder.AppendLine("                resizable: false,");
            CodeBuilder.AppendLine("                height: 630,");
            CodeBuilder.AppendLine("                modal: true,");
            CodeBuilder.AppendLine("                autoOpen: false,");
            CodeBuilder.AppendLine("                width: 700,");
            CodeBuilder.AppendLine("                close: function () {");
            CodeBuilder.AppendLine("                    $(\"#editFrame\").css(\"height\", 150);");
            CodeBuilder.AppendLine("                }");
            CodeBuilder.AppendLine("            });");
            CodeBuilder.AppendLine("            //jquery griddate");
            CodeBuilder.AppendLine("            $(\".LinkToEdit\").click(function () {");
            CodeBuilder.AppendLine("                var src = '<%: Url.Action(\"{0}\", \"{1}\",new{{ id=0 }})  %>';", this.Table.ModelEditName, this.Table.EntityName);
            CodeBuilder.AppendLine("                $(\"#editFrame\").attr(\"src\", src);");
            CodeBuilder.AppendLine("                $('#dialog-edit').dialog('open');");
            CodeBuilder.AppendLine("            });");
            CodeBuilder.AppendLine("        });");
            CodeBuilder.AppendLine("        function ResizeDialog(height) {");
            CodeBuilder.AppendLine("            var titleHeight = $('#dialog-edit').prev(\".ui-dialog-titlebar\").outerHeight();");
            CodeBuilder.AppendLine("            $('#dialog-edit').dialog(\"option\", { \"height\": (parseInt(titleHeight) + parseInt(height)) });");
            CodeBuilder.AppendLine("            if (/chrome\\/([\\d.]+)/.test(navigator.userAgent.toLowerCase())) {");
            CodeBuilder.AppendLine("                $(\"#dialog-edit\").css(\"height\", height + 20);");
            CodeBuilder.AppendLine("            } else {");
            CodeBuilder.AppendLine("                $(\"#dialog-edit\").css(\"height\", height);");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("            $(\"#editFrame\").show();");
            CodeBuilder.AppendLine("            $(\"#editFrame\").css(\"height\", height);");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        function RefreshCurrentForm() {");
            CodeBuilder.AppendLine("            $('#dialog-edit').dialog('close');");
            CodeBuilder.AppendLine("            var urlBaseInfo = '<%: Url.Action(\"{0}\", \"{1}\")  %>';", this.Table.ModelListName, this.Table.EntityName);
            CodeBuilder.AppendLine("            window.location = urlBaseInfo;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        function paging(pgButton) {");
            CodeBuilder.AppendLine("            var data = $(\"#jqTable\").getGridParam(\"rowNum\");");
            CodeBuilder.AppendLine("            var page = $(\"#jqTable\").getGridParam(\"page\");");
            CodeBuilder.AppendLine("            var url = '<%: Url.Action(\"SetListNum\", \"{0}\") %>';", this.Table.EntityName);
            CodeBuilder.AppendLine("            url += \"?num=\" + data + \"&page=\" + page;");
            CodeBuilder.AppendLine("            $.post(url);");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        function showFormatter(cellvalue, options, rowObject) {");
            CodeBuilder.AppendLine("            var rowsinfo = rowObject.toString().split(',');");
            CodeBuilder.AppendLine("            var id = rowsinfo[0].toString();");
            CodeBuilder.AppendLine("            return \"<a  class='LinkToEdit' href='#' onclick='jump(\" + id + \");' >\" + cellvalue + \"</a>\";");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        function jump(id) {");
            CodeBuilder.AppendLine("            var src = '<%: Url.Action(\"{0}\", \"{1}\") %>';", this.Table.ModelEditName, this.Table.EntityName);
            CodeBuilder.AppendLine("            src += \"?id= \" + id;");
            CodeBuilder.AppendLine("            $(\"#editFrame\").attr(\"src\", src);");
            CodeBuilder.AppendLine("            $('#dialog-edit').dialog('open');");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("    </script>");

            CodeBuilder.AppendLine("        <script type=\"text/javascript\">");
            CodeBuilder.AppendLine("            var jqDataUrl = '<%: Url.Action(\"LoadjqData\", \"{0}\") %>';", this.Table.EntityName);
            CodeBuilder.AppendLine("            var lastsel;");
            CodeBuilder.AppendLine("            var colname = \"\";");
            CodeBuilder.AppendLine("            var num = 20;");
            CodeBuilder.AppendLine("            var currentpage = 1;");
            CodeBuilder.AppendLine("            $(document).ready(function () {{");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            $.ajax({");
            CodeBuilder.AppendLine("                url: '<%: Url.Action(\"GetListNum\", \"{0}\") %>',", this.Table.EntityName);
            CodeBuilder.AppendLine("                type:'POST',");
            CodeBuilder.AppendLine("                cache: false,");
            CodeBuilder.AppendLine("                async: false,");
            CodeBuilder.AppendLine("                success: function (data) {");
            CodeBuilder.AppendLine("                    num = data.id;");
            CodeBuilder.AppendLine("                    currentpage = data.page;");
            CodeBuilder.AppendLine("                },");
            CodeBuilder.AppendLine("                error: function (XMLHttpRequest, textStatus, errorThrown) {");
            CodeBuilder.AppendLine("                    num = 20;");
            CodeBuilder.AppendLine("                    currentpage = 1;");
            CodeBuilder.AppendLine("                }");
            CodeBuilder.AppendLine("            });");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                // Set up the jquery grid");
            CodeBuilder.AppendLine("                $(\"#jqTable\").jqGrid({{");
            CodeBuilder.AppendLine("                    // Grid caption");
            CodeBuilder.AppendLine("                    caption: \"\",");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Ajax related configurations");
            CodeBuilder.AppendLine("                    url: jqDataUrl,");
            CodeBuilder.AppendLine("                    datatype: \"json\",");
            CodeBuilder.AppendLine("                    mtype: \"GET\",");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Specify the column names");

            int index = 0;
            List<FieldInfo> listFields = new List<FieldInfo>();
            foreach (var field in this.Table.Fields)
            {
                index++;
                if (tableAddin.IsListField(field.FieldName))
                    listFields.Add(field);
            }

            StringBuilder sb = new StringBuilder();
            index = 0;
            foreach (var field in listFields)
            {
                index++;
                sb.AppendFormat("\"{0}\",", tableAddin.GetFieldDisplayName(field.FieldName));
            }

            CodeBuilder.AppendLine("                    colNames: [{0}],", sb.ToString());
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Configure the columns");
            CodeBuilder.AppendLine("                    colModel: [");
            index = 0;
            foreach (var field in listFields)
            {
                index++;
                if (index == 1)
                {
                    CodeBuilder.AppendLine("                    {{ name: \"{0}\", index: \"{1}\", width: 80, align: \"left\", formatter: \"showlink\", sortable: false, formatoptions:", tableAddin.GetFieldDisplayName(field.FieldName), field.FieldName);
                    CodeBuilder.AppendLine("                        {{ baseLinkUrl: '<%: Url.Action(\"{0}\", \"{1}\") %>' }}", this.Table.ViewEditName, this.Table.EntityName);
                    CodeBuilder.AppendLine("                    }},");

                }
                else
                {
                    if (index < listFields.Count)
                        CodeBuilder.AppendLine("                    {{ name: \"{0}\", index: \"{1}\", width: 15, align: \"center\", sortable: false }},", tableAddin.GetFieldDisplayName(field.FieldName), field.FieldName);
                    else
                        CodeBuilder.AppendLine("                    {{ name: \"{0}\", index: \"{1}\", width: 30, align: \"center\", sortable: false }} ", tableAddin.GetFieldDisplayName(field.FieldName), field.FieldName);
                }
            }


            CodeBuilder.AppendLine("                    ],");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Grid total width and height");
            CodeBuilder.AppendLine("                    width: 760,");
            CodeBuilder.AppendLine("                    height: 400,");
            CodeBuilder.AppendLine("                    // Paging");
            CodeBuilder.AppendLine("                    toppager: true,");
            CodeBuilder.AppendLine("                    page: currentpage,");
            CodeBuilder.AppendLine("                    pager: $(\"#jqTablePager\"),");
            CodeBuilder.AppendLine("                    onPaging: paging,");
            CodeBuilder.AppendLine("                    rowNum: num,");
            CodeBuilder.AppendLine("                    rowList: [10, 20, 50],");
            CodeBuilder.AppendLine("                    viewrecords: true, // Specify if \"total number of records\" is displayed");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Default sorting");
            CodeBuilder.AppendLine("                    sortname: \"DisplayNo\",");
            CodeBuilder.AppendLine("                    sortorder: \"asc\"");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                }}).navGrid(\"#jqTablePager\",");
            CodeBuilder.AppendLine("                    {{ refresh: true, add: false, edit: false, del: false }},");
            CodeBuilder.AppendLine("                        {{}}, // settings for edit");
            CodeBuilder.AppendLine("                        {{}}, // settings for add");
            CodeBuilder.AppendLine("                        {{}}, // settings for delete");
            CodeBuilder.AppendLine("                        {{sopt: [\"cn\"]}} // Search options. Some options can be set on column level");
            CodeBuilder.AppendLine("                 );");
            CodeBuilder.AppendLine("            }});");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        </script>");


            CodeBuilder.AppendLine("    <section>");
            CodeBuilder.AppendLine("        <h3>{0}一覧</h3>", tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <div>");

            CodeBuilder.AppendLine("        <table id=\"jqTable\" class=\"scroll\"></table>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <div id=\"dialog-edit\" style=\"display: none; padding-top: 0px; margin-top: 0px;");
            CodeBuilder.AppendLine("        background-color: rgb(244, 240, 236);\" title=\"{0}登録\">", tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("        <iframe id=\"editFrame\" name=\"editFrame\" frameborder=\"0\" marginwidth=\"0\"");
            CodeBuilder.AppendLine("            marginheight=\"0\" scrolling=\"no\" style=\"width: 674px; height: 150px; margin: 0px;");
            CodeBuilder.AppendLine("            padding: 0px; overflow: hidden;\"></iframe>");
            CodeBuilder.AppendLine("    </div>");

            CodeBuilder.AppendLine("</asp:Content>");

        }

        private void create_masterlistContent2()
        {

            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);



            CodeBuilder.AppendLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"ContentPlaceRight\" runat=\"server\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("  <% using (Html.BeginForm())");
            CodeBuilder.AppendLine("    {{%>");
            CodeBuilder.AppendLine("    <section >");
            CodeBuilder.AppendLine("        <h3 style=\"text-align: center\">操作メニュー</h3>");
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <div class=\"SearchContentTable\">");
            CodeBuilder.AppendLine("        <p><img  src=\"<%:Url.Content(\"~/Content/images/NewEdit16.png\") %>\"/>");
            CodeBuilder.AppendLine("        <a class=\"LinkToEdit\" href=\"#\">{0}を新規作成する</a>", tableAddin.TableDisplayName);
            //CodeBuilder.AppendLine("        <%= Html.ActionLink(\"{2}を新規作成する\", \"{0}\", \"{1}\", new {{ id = 0 }}, new {{ class=\"LinkToEdit\" }})%>", this.Table.ViewEditName, this.Table.EntityName, tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("        </p>");
            CodeBuilder.AppendLine("        <!-- オーダー変更 start -->");
            CodeBuilder.AppendLine("        <div id=\"dialog-form\" title=\"順序変更\" style=\"display: none;\">");
            CodeBuilder.AppendLine("            <p class=\"validateTips\">");
            CodeBuilder.AppendLine("            </p>");
            CodeBuilder.AppendLine("            <div>");
            CodeBuilder.AppendLine("                <script type=\"text/javascript\">");
            CodeBuilder.AppendLine("                    // コントロール名の指定");
            CodeBuilder.AppendLine("                    var OrderListPageUrl = '<%: Url.Action(\"PostExecOrderjqData\",\"{0}\") %>';", this.Table.EntityName);
            CodeBuilder.AppendLine("                </script>");
            CodeBuilder.AppendLine("                <div style=\"display: table-cell; vertical-align: middle;\">");
            CodeBuilder.AppendLine("                    <h3>{0}</h3>", tableAddin.GetFieldDisplayName(this.Table.GetDisplayValueField().FieldName));
            CodeBuilder.AppendLine("                    <%= Html.DropDownListFor(m => m.OrderList,");
            CodeBuilder.AppendLine("                        new SelectList(Model.Items, \"Value\", \"Text\"),");
            CodeBuilder.AppendLine("                        new {{size=2, id = \"OrderList\",name = \"OrderList\" ,style = \"width:400px;height: 240px;\"}})%>");
            CodeBuilder.AppendLine("                </div>");
            CodeBuilder.AppendLine("                <div style=\"padding-left: 20px; display: table-cell; vertical-align: middle;\">");
            CodeBuilder.AppendLine("                    <p>");
            CodeBuilder.AppendLine("                        <a id=\"upOrder\">▲</a></p>");
            CodeBuilder.AppendLine("                    <p>");
            CodeBuilder.AppendLine("                        <a id=\"downOrder\">▼</a></p>");
            CodeBuilder.AppendLine("                </div>");
            CodeBuilder.AppendLine("            </div>");
            CodeBuilder.AppendLine("        </div>");
            CodeBuilder.AppendLine("        <!-- オーダー変更 end -->");
            CodeBuilder.AppendLine("        <p>");
            CodeBuilder.AppendLine("            <img  src=\"<%:Url.Content(\"~/Content/images/NewEdit16.png\") %>\"/><a id=\"OrderChangeLink\"  href=\"#\">{0}の順序を変更する</a>", tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("        </p>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <section >");
            CodeBuilder.AppendLine("        <h3 style=\"text-align: center\">検索</h3>");
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <%= Html.ValidationSummary()%>");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <div class=\"SearchContentTable\">");
            CodeBuilder.AppendLine("");

            foreach (var field in this.Table.Fields)
            {
                if (tableAddin.IsSearchField(field.FieldName))
                {
                    CodeBuilder.AppendLine("        <p>{0}<br />", tableAddin.GetFieldDisplayName(field.FieldName));
                    CodeBuilder.AppendLine("            {0}", getSingleHTMLDeclare(field));
                    CodeBuilder.AppendLine("        </p>");
                }
            }

            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        <p>");
            CodeBuilder.AppendLine("        <input id=\"Submit1\" type=\"submit\" value=\"検索\" runat=\"server\"  style = \"padding:5px 25px; \"/>");
            CodeBuilder.AppendLine("        <input id=\"Button1\" type=\"button\" value=\"クリア\" onclick=\"location.href='<%: Url.Action(\"{0}\", \"{1}\") %>'\"  style = \"padding:5px 20px;\" />", this.Table.ViewListName, this.Table.EntityName);
            CodeBuilder.AppendLine("        </p>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <% }} %>");
            CodeBuilder.AppendLine("</asp:Content>");

        }

        #endregion

        #region master edit
        private void create_masteredit()
        {

            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            FieldInfo keyField = this.Table.GetKeyField();


            CodeBuilder.AppendLine("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/SiteBlank.Master\"");
            CodeBuilder.AppendLine("    Inherits=\"System.Web.Mvc.ViewPage<MvcApplication.Models.{0}>\" %>", this.Table.ModelEditName);
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"MainContent\" runat=\"server\">");
            CodeBuilder.AppendLine("    <style type=\"text/css\">");
            CodeBuilder.AppendLine("        .field-validation-error");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            color: Red;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        .tooltipContent");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            height: 30px;");
            CodeBuilder.AppendLine("            text-align: right;");
            CodeBuilder.AppendLine("            padding-right: 10px;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        .tooltip .button");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            min-width: 70px;");
            CodeBuilder.AppendLine("            height: 30px;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        .ContentTable");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            width: 650px;");
            CodeBuilder.AppendLine("            margin: 10px 10px 10px 10px;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        .ContentTable .RowHeader");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            width: 25%;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        .RowContent span");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            padding-left: 5px;");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        Body");
            CodeBuilder.AppendLine("        {");
            CodeBuilder.AppendLine("            background-image: none;");
            CodeBuilder.AppendLine("            background-color: rgb(244, 240, 236);");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("    </style>");
            CodeBuilder.AppendLine("    <script type=\"text/javascript\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        function RefreshWin() {");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            var win = window.parent;");
            CodeBuilder.AppendLine("            if (win == null)");
            CodeBuilder.AppendLine("                return;");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            win.RefreshCurrentForm();");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("        jQuery(function ($) {");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            var status = '<%= ViewData[\"closepopup\"]  %>';");
            CodeBuilder.AppendLine("            if (status == \"1\") {");
            CodeBuilder.AppendLine("                RefreshWin();");
            CodeBuilder.AppendLine("                return;");
            CodeBuilder.AppendLine("            }");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            $(\"#{0}\").focus();", this.Table.GetDisplayValueField().FieldName);
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            $('#btnRegister').live(\"click\", function () {");
            CodeBuilder.AppendLine("                var parent = window.parent;");
            CodeBuilder.AppendLine("                parent.jConfirm('登録します。よろしいですか？', '登録', function (r) {");
            CodeBuilder.AppendLine("                    if (r) {");
            CodeBuilder.AppendLine("                        $('#btnName').val(\"登録\");");
            CodeBuilder.AppendLine("                        document.forms[0].submit();");
            CodeBuilder.AppendLine("                    }");
            CodeBuilder.AppendLine("                });");
            CodeBuilder.AppendLine("            });");
            CodeBuilder.AppendLine("            $('#btnDel').live(\"click\", function () {");
            CodeBuilder.AppendLine("                var parent = window.parent;");
            CodeBuilder.AppendLine("                var url = '<%: Url.Action(\"DeleteJudge\", \"{0}\") %>';",this.Table.EntityName);
            CodeBuilder.AppendLine("                $.post(url, function (data) {");
            CodeBuilder.AppendLine("                    var flg = data.id;");
            CodeBuilder.AppendLine("                    if (flg) {");
            CodeBuilder.AppendLine("                        parent.jConfirm('削除します。よろしいですか？', '登録', function (r) {");
            CodeBuilder.AppendLine("                            if (r) {");
            CodeBuilder.AppendLine("                                var url = '<%: Url.Action(\"Delete\", \"{0}\") %>';", this.Table.EntityName);
            CodeBuilder.AppendLine("                                $.post(url, function (data) {");
            CodeBuilder.AppendLine("                                    RefreshWin();");
            CodeBuilder.AppendLine("                                });");
            CodeBuilder.AppendLine("                            }");
            CodeBuilder.AppendLine("                        });");
            CodeBuilder.AppendLine("                    }");
            CodeBuilder.AppendLine("                    else {");
            CodeBuilder.AppendLine("                        parent.jAlert('使用されています。削除できません。', '登録');");
            CodeBuilder.AppendLine("                        return false;");
            CodeBuilder.AppendLine("                    }");
            CodeBuilder.AppendLine("                });");
            CodeBuilder.AppendLine("                return false;");
            CodeBuilder.AppendLine("            });");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        });");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        function ResizeParentDialogSize() {");
            CodeBuilder.AppendLine("            var parent = window.parent;");
            CodeBuilder.AppendLine("            curBrower = new CurBrower();");
            CodeBuilder.AppendLine("            var height = curBrower.bodyHeight;");
            CodeBuilder.AppendLine("            parent.ResizeDialog(height);");
            CodeBuilder.AppendLine("        }");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        $(document).ready(function () {");
            CodeBuilder.AppendLine("            ResizeParentDialogSize();");
            CodeBuilder.AppendLine("        });");
            CodeBuilder.AppendLine("    </script>");

            CodeBuilder.AppendLine("    <% using (Html.BeginForm())");
            CodeBuilder.AppendLine("       {{%>");
            CodeBuilder.AppendLine("    <div id=\"tbl\" class=\"ContentTable\">");

            foreach (var field in this.Table.Fields)
            {
                if (field == keyField)
                    continue;

                CodeBuilder.AppendLine("        <div class=\"ContentRow\">");
                CodeBuilder.AppendLine("            <div class=\"RowHeader\">");
                CodeBuilder.AppendLine("                {0}", tableAddin.GetFieldDisplayName(field.FieldName));
                CodeBuilder.AppendLine("            </div>");
                CodeBuilder.AppendLine("            <div class=\"RowContent\">");
                CodeBuilder.AppendLine("                <p>");
                CodeBuilder.AppendLine("                    {0}</p>", getSingleHTMLDeclare(field));
                CodeBuilder.AppendLine("                <p class=\"TextNav\">");
                CodeBuilder.AppendLine("                    {0}</p>", getSingleTextNavString(field));
                CodeBuilder.AppendLine("                <%= Html.ValidationMessageFor(m => m.{0})%>", field.FieldName);
                CodeBuilder.AppendLine("            </div>");
                CodeBuilder.AppendLine("        </div>");
            }

            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <div class=\"tooltipContent\">");
            CodeBuilder.AppendLine("        <div class=\"tooltip\">");
            CodeBuilder.AppendLine("            <input type=\"button\" id=\"btnRegister\" class=\"button\" name=\"btnName\"  value=\"登録\" />");
            CodeBuilder.AppendLine("        <% if (Model.{0} > 0){{ %>",this.Table.GetKeyField().FieldName);
            CodeBuilder.AppendLine("            <input type=\"button\" id=\"btnDel\" class=\"button\" name=\"btnName\" value=\"削除\" />");
            CodeBuilder.AppendLine("        <% }} %>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnBack\" class=\"button\"  name=\"btnName\" value=\"キャンセル\" />");
            CodeBuilder.AppendLine("            <%= Html.HiddenFor(m => m.btnName)%> ");         
            CodeBuilder.AppendLine("        </div>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <div style=\"height: 40px;\">");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <%}} %>");
            CodeBuilder.AppendLine("</asp:Content>");

        }
        #endregion

        #region common list
        private void create_commonlist()
        {
            create_commonlistdeclare();
            create_commonlistContent1();
            create_commonlistContent2();
        }
        private void create_commonlistdeclare()
        {
            CodeBuilder.AppendLine("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/Divided2Layout.Master\" Inherits=\"System.Web.Mvc.ViewPage<MvcApplication.Models.{0}>\" %>", this.Table.ModelListSearchName);
        }

        private void create_commonlistContent1()
        {

            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;


            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);

            CodeBuilder.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"ContentPlaceLeft\" runat=\"server\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <section>");
            CodeBuilder.AppendLine("        <h3>{0}一覧</h3>", tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <div>");
            CodeBuilder.AppendLine("        <script type=\"text/javascript\">");
            CodeBuilder.AppendLine("            var jqDataUrl = '<%: Url.Action(\"LoadjqData\", \"{0}\") %>';", this.Table.EntityName);
            CodeBuilder.AppendLine("            var lastsel;");
            CodeBuilder.AppendLine("            var colname = \"\";");
            CodeBuilder.AppendLine("            $(document).ready(function () {{");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                // Set up the jquery grid");
            CodeBuilder.AppendLine("                $(\"#jqTable\").jqGrid({{");
            CodeBuilder.AppendLine("                    // Grid caption");
            CodeBuilder.AppendLine("                    caption: \"\",");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Ajax related configurations");
            CodeBuilder.AppendLine("                    url: jqDataUrl,");
            CodeBuilder.AppendLine("                    datatype: \"json\",");
            CodeBuilder.AppendLine("                    mtype: \"GET\",");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Specify the column names");

            List<FieldInfo> listFields = new List<FieldInfo>();
            int index = 0;
            foreach (var field in this.Table.Fields)
            {
                index++;
                if (tableAddin.IsListField(field.FieldName))
                    listFields.Add(field);
            }

            index = 0;
            foreach (var r in res)
            {
                index++;
                if (!tableAddin.IsListField(r.ForeignTable.KeyFieldName))
                    continue;

                TableInfo tTable = this.Connection.FindTable(r.PrimaryTable.TableName);
                FieldInfo tField = tTable.FindField(r.PrimaryTable.ValueFieldName);
                listFields.Add(tField);
            }


            StringBuilder sb = new StringBuilder();
            index = 0;
            foreach (var field in listFields)
            {
                index++;
                sb.AppendFormat("\"{0}\",", tableAddin.GetFieldDisplayName(field.FieldName));
            }

            CodeBuilder.AppendLine("                    colNames: [{0}],", sb.ToString());
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Configure the columns");
            CodeBuilder.AppendLine("                    colModel: [");
            index = 0;
            foreach (var field in listFields)
            {
                index++;
                if (index == 1)
                {
                    CodeBuilder.AppendLine("                    {{ name: \"{0}\", index: \"{1}\", width: 80, align: \"left\", formatter: \"showlink\", sortable: false, formatoptions:", tableAddin.GetFieldDisplayName(field.FieldName), field.FieldName);
                    CodeBuilder.AppendLine("                        {{ baseLinkUrl: '<%: Url.Action(\"{0}\", \"{1}\") %>' }}", this.Table.ViewEditName, this.Table.EntityName);
                    CodeBuilder.AppendLine("                    }},");

                }
                else
                {
                    if (index < listFields.Count)
                        CodeBuilder.AppendLine("                    {{ name: \"{0}\", index: \"{1}\", width: 15, align: \"center\", sortable: false }},", tableAddin.GetFieldDisplayName(field.FieldName), field.FieldName);
                    else
                        CodeBuilder.AppendLine("                    {{ name: \"{0}\", index: \"{1}\", width: 30, align: \"center\", sortable: false }} ", tableAddin.GetFieldDisplayName(field.FieldName), field.FieldName);
                }
            }


            CodeBuilder.AppendLine("                    ],");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Grid total width and height");
            CodeBuilder.AppendLine("                    width: 760,");
            CodeBuilder.AppendLine("                    height: 400,");
            CodeBuilder.AppendLine("                    // Paging");
            CodeBuilder.AppendLine("                    toppager: true,");
            CodeBuilder.AppendLine("                    pager: $(\"#jqTablePager\"),");
            CodeBuilder.AppendLine("                    rowNum: 10,");
            CodeBuilder.AppendLine("                    rowList: [10, 20, 50],");
            CodeBuilder.AppendLine("                    viewrecords: true, // Specify if \"total number of records\" is displayed");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                    // Default sorting");
            CodeBuilder.AppendLine("                    sortname: \"{0}\",", this.Table.GetKeyField());
            CodeBuilder.AppendLine("                    sortorder: \"asc\"");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("                }}).navGrid(\"#jqTablePager\",");
            CodeBuilder.AppendLine("                    {{ refresh: true, add: false, edit: false, del: false }},");
            CodeBuilder.AppendLine("                        {{}}, // settings for edit");
            CodeBuilder.AppendLine("                        {{}}, // settings for add");
            CodeBuilder.AppendLine("                        {{}}, // settings for delete");
            CodeBuilder.AppendLine("                        {{sopt: [\"cn\"]}} // Search options. Some options can be set on column level");
            CodeBuilder.AppendLine("                 );");
            CodeBuilder.AppendLine("            }});");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        </script>");
            CodeBuilder.AppendLine("        <table id=\"jqTable\" class=\"scroll\"></table>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("</asp:Content>");

        }

        private void create_commonlistContent2()
        {

            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);



            CodeBuilder.AppendLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"ContentPlaceRight\" runat=\"server\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("  <% using (Html.BeginForm())");
            CodeBuilder.AppendLine("    {{%>");
            CodeBuilder.AppendLine("    <section>");
            CodeBuilder.AppendLine("        <h3>操作</h3>");
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <div class=\"SearchContentTable\">");
            CodeBuilder.AppendLine("        <p><img  src=\"../../Content/images/New Edit.png\"/>");
            CodeBuilder.AppendLine("        <a class=\"LinkToEdit\" href=\"#\">{0}を新規作成する</a>", tableAddin.TableDisplayName);
            //CodeBuilder.AppendLine("        <%= Html.ActionLink(\"{2}を新規作成する\", \"{0}\", \"{1}\", new {{ id = 0 }}, new {{  }})%>", this.Table.ViewEditName, this.Table.EntityName, tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("        </p>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <section>");
            CodeBuilder.AppendLine("        <h3>検索</h3>");
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <%= Html.ValidationSummary()%>");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <div class=\"SearchContentTable\">");
            CodeBuilder.AppendLine("");

            foreach (var field in this.Table.Fields)
            {
                if (tableAddin.IsSearchField(field.FieldName))
                {
                    CodeBuilder.AppendLine("        <p>{0}<br />", tableAddin.GetFieldDisplayName(field.FieldName));
                    CodeBuilder.AppendLine("            {0}", getSingleHTMLDeclare(field));
                    CodeBuilder.AppendLine("        </p>");
                }
            }

            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        <p>");
            CodeBuilder.AppendLine("        <input id=\"Submit1\" type=\"submit\" value=\"検索\" runat=\"server\" style = \"padding:5px 25px; \" />");
            CodeBuilder.AppendLine("        <input id=\"Button1\" type=\"button\" value=\"クリア\" onclick=\"location.href='<%: Url.Action(\"{0}\", \"{1}\") %>'\"  style = \"padding:5px 20px;\"/>", this.Table.ViewListName, this.Table.EntityName);
            CodeBuilder.AppendLine("        </p>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("    <% }} %>");
            CodeBuilder.AppendLine("</asp:Content>");

        }

        #endregion

        #region common edit
        private void create_commonedit()
        {
            CodeBuilder.AppendLine("<%@ Page Title=\"\" Language=\"C#\" MasterPageFile=\"~/Views/Shared/BasicLayout.Master\" Inherits=\"System.Web.Mvc.ViewPage<MvcApplication.Models.{0}>\"  %>", this.Table.ModelEditName);
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"ContentPlaceCenter\" runat=\"server\">");
            create_commonedit01();
            create_commonedit02();
            create_commonedit03();
            CodeBuilder.AppendLine("</asp:Content>");

        }

        private void create_commonedit01()
        {
            CodeBuilder.AppendLine("    <style type=\"text/css\">");
            CodeBuilder.AppendLine("    .tooltip span {{");
            CodeBuilder.AppendLine("     background: #ffc;");
            CodeBuilder.AppendLine("     border: 1px solid #fc6;");
            CodeBuilder.AppendLine("     height: 45px;");
            CodeBuilder.AppendLine("     position: absolute;");
            CodeBuilder.AppendLine("     top: -85px;");
            CodeBuilder.AppendLine("     left: 0;");
            CodeBuilder.AppendLine("     text-align: center;");
            CodeBuilder.AppendLine("     padding: 20px 12px 10px;");
            CodeBuilder.AppendLine("     z-index: 2;");
            CodeBuilder.AppendLine("     display: none;");
            CodeBuilder.AppendLine("     }}");
            CodeBuilder.AppendLine("        .Table");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            width:500px;");
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        .Table .rowHeader");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            display :table-row;");
            CodeBuilder.AppendLine("            border : thin solid DarkGray;");
            CodeBuilder.AppendLine("            background-color:#00FFFF;");
            CodeBuilder.AppendLine("            vertical-align : middle;");
            CodeBuilder.AppendLine("            text-align:center;");
            CodeBuilder.AppendLine("            width:100%;");
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("        .Table .rowContent");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            display :table-row;");
            CodeBuilder.AppendLine("            border : midium solid DarkGray;");
            CodeBuilder.AppendLine("            vertical-align : middle;");
            CodeBuilder.AppendLine("            text-align:center;");
            CodeBuilder.AppendLine("            width:100%;");
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("        .Table .rowHeader .Column1");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            display : table-cell;");
            CodeBuilder.AppendLine("            border : thin solid DarkGray;");
            CodeBuilder.AppendLine("            vertical-align : middle;");
            CodeBuilder.AppendLine("            background-color:#ADD8E6;");
            CodeBuilder.AppendLine("            text-align:center;");
            CodeBuilder.AppendLine("            width:50%;");
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("        .Table .rowContent .Column1");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            display : table-cell;");
            CodeBuilder.AppendLine("            border : thin solid DarkGray;");
            CodeBuilder.AppendLine("            vertical-align : middle;");
            CodeBuilder.AppendLine("            text-align:justify;");
            CodeBuilder.AppendLine("            width:50%;");
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        .Table .rowContent select");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            width:98%;");
            CodeBuilder.AppendLine("            }}");
            CodeBuilder.AppendLine("        .Table .rowContent input[type=\"text\"]");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            width:90%;");
            CodeBuilder.AppendLine("            margin:0px 1px 0px 1px;");
            CodeBuilder.AppendLine("        }}");
            CodeBuilder.AppendLine("        ");
            CodeBuilder.AppendLine("            ");
            CodeBuilder.AppendLine("        .tooltipContent");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("             height:30px;    ");
            CodeBuilder.AppendLine("        }}    ");
            CodeBuilder.AppendLine("        .tooltip");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("            float:right;");
            CodeBuilder.AppendLine("        }}");
            CodeBuilder.AppendLine("        .tooltip .button");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("             min-width :70px;");
            CodeBuilder.AppendLine("             height:30px;    ");
            CodeBuilder.AppendLine("        }}        ");
            CodeBuilder.AppendLine("        ");
            CodeBuilder.AppendLine("        .field-validation-error");
            CodeBuilder.AppendLine("        {{");
            CodeBuilder.AppendLine("             color:Red;    ");
            CodeBuilder.AppendLine("        }}        ");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    </style>");

        }
        private void create_commonedit02()
        {
            CodeBuilder.AppendLine("    <script type=\"text/javascript\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("        $(\"#btnDel,#btnDel2\").live(\"click\", function () {{");
            CodeBuilder.AppendLine("            var msgX = 'データを削除します、よろしいでしょうか？';");
            CodeBuilder.AppendLine("            return confirm(msgX);");
            CodeBuilder.AppendLine("        }});");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    </script>");

        }
        private void create_commonedit03()
        {

            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            FieldInfo keyField = this.Table.GetKeyField();

            CodeBuilder.AppendLine("    <% using (Html.BeginForm())");
            CodeBuilder.AppendLine("       {{%>");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <%: Html.ValidationSummary(true, \"入力内容に誤りがあります。エラーを修正し、再試行してください。\")%>");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <section>");
            CodeBuilder.AppendLine("        <h3>{0}詳細</h3>", tableAddin.TableDisplayName);
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <div class=\"tooltipContent\">");
            CodeBuilder.AppendLine("        <div class=\"tooltip\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnTemp\" class=\"button\" name=\"btnName\"  value=\"一時保存\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を一時保存します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnRegister\" class=\"button\" name=\"btnName\"  value=\"登録\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を登録します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnAgreen\" class=\"button\" name=\"btnName\"  value=\"承認\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を承認します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnUnAgreen\" class=\"button\" name=\"btnName\"  value=\"承認取消\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を承認取消します。</span>");
            CodeBuilder.AppendLine("           <input type=\"submit\" id=\"btnCopy\" class=\"button\" name=\"btnName\"  value=\"コピーして作成\"  />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴をコピーして作成します。</span>");
            CodeBuilder.AppendLine("            <input type=\"button\" id=\"btnPrint\" class=\"button\"  name=\"btnName\" value=\"印刷\"   />");
            CodeBuilder.AppendLine("            <span  >編集中の養殖履歴を日本語で印刷します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnDel\" class=\"button\" name=\"btnName\"  value=\"削除\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を削除します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnBack\" class=\"button\"  name=\"btnName\" value=\"戻る\" />");
            CodeBuilder.AppendLine("            <span>養殖履歴一覧に戻ります。</span>");
            CodeBuilder.AppendLine("        </div>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <section>");
            CodeBuilder.AppendLine("        <h3>セクション</h3><br />");
            CodeBuilder.AppendLine("        <hr />");
            CodeBuilder.AppendLine("        <div  Class=\"ContentTable\">");

            foreach (var field in this.Table.Fields)
            {
                if (field == keyField)
                    continue;

                CodeBuilder.AppendLine("            <div class=\"ContentRow\">");
                CodeBuilder.AppendLine("                <div class=\"RowHeader\">");
                CodeBuilder.AppendLine("                    {0}", tableAddin.GetFieldDisplayName(field.FieldName));
                CodeBuilder.AppendLine("                </div>");
                CodeBuilder.AppendLine("                <div class=\"RowContent\">");
                CodeBuilder.AppendLine("                    {0}", getSingleHTMLDeclare(field));
                CodeBuilder.AppendLine("                    <p class=\"TextNav\">{0}</p>", getSingleTextNavString(field));
                CodeBuilder.AppendLine("                    <%= Html.ValidationMessageFor(m => m.{0})%>", field.FieldName);
                CodeBuilder.AppendLine("                </div>");
                CodeBuilder.AppendLine("            </div>");
            }
            CodeBuilder.AppendLine("        </div> ");
            CodeBuilder.AppendLine("    </section>");
            CodeBuilder.AppendLine("    <hr />");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <div class=\"tooltipContent\">");
            CodeBuilder.AppendLine("        <div class=\"tooltip\">");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnTemp2\" class=\"button\" name=\"btnName\"  value=\"一時保存\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を一時保存します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnRegister2\" class=\"button\" name=\"btnName\"  value=\"登録\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を登録します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnAgreen2\" class=\"button\" name=\"btnName\"  value=\"承認\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を承認します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnUnAgreen2\" class=\"button\" name=\"btnName\"  value=\"承認取消\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を承認取消します。</span>");
            CodeBuilder.AppendLine("           <input type=\"submit\" id=\"btnCopy2\" class=\"button\" name=\"btnName\"  value=\"コピーして作成\"  />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴をコピーして作成します。</span>");
            CodeBuilder.AppendLine("            <input type=\"button\" id=\"btnPrint2\" class=\"button\"  name=\"btnName\" value=\"印刷\"   />");
            CodeBuilder.AppendLine("            <span  >編集中の養殖履歴を日本語で印刷します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnDel2\" class=\"button\" name=\"btnName\"  value=\"削除\" />");
            CodeBuilder.AppendLine("            <span>編集中の養殖履歴を削除します。</span>");
            CodeBuilder.AppendLine("            <input type=\"submit\" id=\"btnBac2k\" class=\"button\"  name=\"btnName\" value=\"戻る\" />");
            CodeBuilder.AppendLine("            <span>養殖履歴一覧に戻ります。</span>");
            CodeBuilder.AppendLine("        </div>");
            CodeBuilder.AppendLine("    </div>");
            CodeBuilder.AppendLine("");
            CodeBuilder.AppendLine("    <% }} %>");

        }

        #endregion

        private string getSingleHTMLDeclare(FieldInfo field)
        {
            string htmlType = "";

            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;

            bool isForeignKey = false;
            TableInfo t = null;
            foreach (var r in res)
            {
                if (r.PrimaryTable.KeyFieldName.ToLower().CompareTo(field.FieldName.ToLower()) == 0)
                {
                    isForeignKey = true;
                    t = Connection.FindTable(r.PrimaryTable.TableName);
                    break;
                }
            }


            switch (field.FieldType.MapName.ToLower())
            {
                case "int":
                    if (!isForeignKey)
                        htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumText\", maxLength = \"10\" }})%>", field.FieldName);
                    else
                        htmlType = string.Format("<%= Html.DropDownListFor(m => m.{0}, new SelectList(Model.{1}, \"Value\", \"Text\", Model.{0}))%>", field.FieldName, t.EntityListName);

                    break;
                case "datetime":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"datepicker\", maxLength = \"10\" }})%>", field.FieldName);
                    break;
                case "decimal":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumText\", maxLength = \"10\" }})%>", field.FieldName);
                    break;
                case "bool":
                    htmlType = string.Format("<%= Html.CheckBoxFor(m => m.{0})%>", field.FieldName);
                    break;
                case "string":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"Fullwidth\", maxLength = \"{1}\" }})%>", field.FieldName, field.FieldSize);
                    break;
                case "double":
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumText\", maxLength = \"10\" }})%>", field.FieldName);
                    break;
                default:
                    htmlType = string.Format("<%= Html.TextBoxFor(m => m.{0},new {{ @class = \"NumABC\", maxLength = \"{1}\" }})%>", field.FieldName, field.FieldSize);
                    break;
            }
            return htmlType;
        }

        private string getSingleTextNavString(FieldInfo field)
        {
            string textNav = "";

            var res = from s in this.Connection.Relations
                      where
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0
                      select s;

            bool isForeignKey = false;
            TableInfo t = null;
            foreach (var r in res)
            {
                if (r.PrimaryTable.KeyFieldName.ToLower().CompareTo(field.FieldName.ToLower()) == 0)
                {
                    isForeignKey = true;
                    t = Connection.FindTable(r.PrimaryTable.TableName);
                    break;
                }
            }


            switch (field.FieldType.MapName.ToLower())
            {
                case "int":
                    if (!isForeignKey)
                        textNav = "半角英数字で入力してください。";
                    else
                        textNav = "";
                    break;
                case "datetime":
                    textNav = "半角英数字で入力してください。";
                    break;
                case "decimal":
                    textNav = "半角英数字で入力してください。";
                    break;
                case "bool":
                    textNav = "半角英数字で入力してください。";
                    break;
                case "string":
                    textNav = "全角文字で入力して下さい。";
                    break;
                case "double":
                    textNav = "半角英数字で入力してください。";
                    break;
                default:
                    textNav = "半角英数字で入力してください。";
                    break;
            }
            return textNav;
        }
    }
}
